/**
 * @author LKQ
 * @date 2021/12/7 17:14
 * @description 递归
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums =  {-10, -7, -5, -4, -3, -2, 0, 5, 8, 9};
        Solution solution = new Solution();
        solution.sortedArrayToBST(nums);
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums, 0, nums.length - 1 );
    }

    public TreeNode helper(int[] nums, int left, int right){
        if (left > right) {
            return null;
        }
        // 总是选择中间位置左边的数字作为根节点
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = helper(nums, left, mid - 1);
        root.right = helper(nums, mid + 1, right);
        return root;
    }
}
